﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;
using YTCMS.Domain.Entities;
using YTCMS.Services.Services;

namespace Admin.Controllers
{
    public class GuideController : BaseController
    {
        private readonly IguideService _guide;
        public GuideController(IguideService guide)
        {
            this._guide = guide;
        }
        //
        // GET: /Guide/
        public ActionResult Index(int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount = 0;
            var model = _guide.PageList(s => true, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return View(model);
        }
        public ActionResult Search(string pname, int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount = 0;
            var model = _guide.GetMany(s => true);
            model = QueryHelper.WhereIf<guide>(model, s => s.Title.Contains(pname), !string.IsNullOrWhiteSpace(pname));
            model = _guide.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return PartialView("List", model);
        }

        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(guide guide)
        {
            if (ModelState.IsValid)
            {
                guide.PushTime = System.DateTime.Now;
                 _guide.Add(guide);
                 return RedirectToAction("Index");
            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            var model = _guide.Get(s => s.Id == id);
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(guide guide)
        {
            if (ModelState.IsValid)
            {
                _guide.Update(guide);
                return RedirectToAction("Index");
            }
            return View();
        }

        public JsonResult Delete(int id)
        {
            var model = _guide.Get(s => s.Id == id);
            _guide.Delete(model);
            return new JsonResult() { Data = true, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
	}
}